<template>
    <div>
        <el-row :gutter="20" style="background-color: #f5f7fa; margin: 10px 0px;">
            <el-col :span="8"><h3>首页banner管理</h3></el-col>
            <el-col :span="8"><h3 style="color: #f5f7fa;">加油，努力！</h3></el-col>
            <el-col :span="4"><el-button type="primary" @click="initPageData" style="margin: 10px 10px;">
                <i class="fa fa-refresh"></i> 刷新数据</el-button></el-col>
            <el-col :span="4"><el-button @click="addBannerClick" type="primary" style="margin: 10px 10px;">
                <i class="fa fa-plus"></i> 新增banner</el-button></el-col>
        </el-row>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    type="index"
                    label="显示顺序"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="banner标题"
                    width="180">
            </el-table-column>
            <el-table-column
                    label="banner图片"
                    width="180">
                <template slot-scope="scope">
                    <template v-if="scope.row.path != ''">
                        <img :src="scope.row.path" width="100%" />
                    </template>
                    <template v-else>
                        无封面图
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                    prop="link"
                    label="落地页链接"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="sort"
                    label="排序"
                    width="100">
            </el-table-column>
            <el-table-column
                    prop="created_at"
                    label="创建时间"
                    width="160">
            </el-table-column>
            <el-table-column
                    prop="updated_at"
                    label="修改时间"
                    width="160">
            </el-table-column>
            <el-table-column
                    label="状态"
                    width="100">
                <template slot-scope="scope">
                    <template v-if="scope.row.status == 1">
                        正常
                    </template>
                    <template v-else>
                        禁用
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="text" @click="editBannerClick(scope.row.id)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="banner信息" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="banner名称" :label-width="formLabelWidth">
                    <el-input v-model="form.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="banner图" :label-width="formLabelWidth">
                    <el-upload
                            class="avatar-uploader"
                            action="/ibe/api/manage/system/banner/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            name="pic"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="form.path" :src="form.path" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="落地页链接" :label-width="formLabelWidth">
                    <el-input v-model="form.link" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序" :label-width="formLabelWidth">
                    <el-input-number v-model="form.sort" :min="1" :max="100"></el-input-number>
                    支持0-100之间的排序，越大排在越前面，不填为默认值0
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">启用</el-radio>
                        <el-radio :label="2">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="postBannerForm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Banner",
        data() {
            return {
                dialogFormVisible: false,
                formLabelWidth: '120px',
                form: {},
                tableData: []
            }
        },
        created(){
            this.initPageData()
            let _this = this;
            // _this.$utilPromptMsg.successMsg('操作成功',_this)
        },
        methods:{
            initPageData(){
                this.getBannerList()
            },
            addBannerClick(){
                this.form = {}
                this.dialogFormVisible = true;
            },
            editBannerClick(id){
                this.form = {}
                this.getBannerInfo(id)
                this.dialogFormVisible = true;
            },
            postBannerForm(){
                let _this = this;
                _this.$Http.post('/manage/system/banner/edit', _this.form, {
                    headers: {}
                }).then(function (response) {
                    if (response.data.code == 0) {
                        _this.dialogFormVisible = false
                        _this.initPageData()

                        _this.$utilPromptMsg.successMsg('操作成功',_this)
                    } else {

                        _this.$utilPromptMsg.errorMsg(response.data.msg,_this)
                    }
                }).catch(function (response) {
                    _this.$utilPromptMsg.errorMsg(response.message,_this)
                });
            },
            getBannerList(){
                let _this = this;
                _this.$Http.get('/manage/system/banner/list', {}, {
                    headers: {}
                }).then(function (response) {
                    console.log('正常', response)
                    if (response.code === 0) {

                        _this.tableData = response.data
                    } else {

                        _this.$Util.errorMsg(response.msg)
                    }
                }).catch(function (response) {
                     console.log('错误日志', response)
                    _this.$Util.errorMsg(response.message)
                });
            },
            getBannerInfo(id){
                let _this = this;
                _this.$Http.get('/manage/system/banner/'+id+'/info', {}, {
                    headers: {}
                }).then(function (response) {
                    if (response.data.code === 0) {
                        _this.form = response.data.data
                    } else {

                        _this.$utilPromptMsg.errorMsg(response.data.msg,_this)
                    }
                }).catch(function (response) {
                    _this.$utilPromptMsg.errorMsg(response.message,_this)
                });
            },
            handleAvatarSuccess(res, file) {
                // 处理正确了把图片现在方框内，否则抛出提示
                if (res.code == 0 && res.data.baseUrl != "") {
                    this.form.picture_id = res.data.pic_id;
                    this.form.path = res.data.pic_url;

                    _this.$utilPromptMsg.successMsg('图片上传成功',this);
                } else {
                    _this.$utilPromptMsg.errorMsg(res.msg,this);
                }
            },
            beforeAvatarUpload(file) {
                // const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                // if (!isJPG) {
                //     _this.$utilPromptMsg.errorMsg('上传头像图片只能是 JPG 格式!',this);
                // }
                if (!isLt2M) {
                    _this.$utilPromptMsg.errorMsg('上传图片大小不能超过 2MB!',this);
                }

                return isLt2M;
            }
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>